﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 5, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };



    //表格数据和分页
   function getList(that) {
       $.ajax({
           type: "POST",
           url: "/Message/Basics",
           data: that,
           dataType: "json",
           success: function (data) {
               var result = JSON.parse(data.Data);
               var usersData = result.DataList;
               pageData.PageCount = result.PageCount;
               pageData.PageNumber = result.PageNumber;
               pageData.PageSize = result.PageSize;
               pageData.RowCount = result.RowCount;
               //执行加载数据的方法
               renderDate(usersData);
               showPage(pageData);
           },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
               layer.alert(textStatus);
           }
       });

       //渲染数据
       function renderDate(list) {
           var dataHtml = '';
           if (list != null) {
               layui.each(list, function (index, item) {
                   dataHtml += '<tr>'
                    + '<td>' + item.Id + '</td>'
                    + '<td align="left">' + item.Name + '</td>'
                    + '<td>' + (item.Sex == 0 ? "男" : "女") + '</td>'
                    + '<td>' + item.Age + '</td>'
                    + '<td>' + item.Nation + '</td>'
                    + '<td>' + item.Education + '</td>'
                    + '<td>' + item.IDNumber + '</td>'
                    + '<td>' + item.Phone + '</td>'
                    + '<td>' + item.Address + '</td>'
                    + '<td>' + item.AddTime + '</td>'
                   + '<td><a class="layui-btn layui-btn-radius layui-btn-primary" id="Update" data-id="' + item.Id + '">修改</a><a class="layui-btn layui-btn-radius layui-btn-primary" id="Removetask"  data-id="' + item.Id + '">删除</a></td>'
                + '</tr>';
               });
           } else {
               dataHtml = '<tr><td colspan="13">暂无数据</td></tr>';
           }
           $(".news_content").html(dataHtml);   //渲染数据
           form.render(); //渲染表单
       }

   }
    //分页
   function showPage(page) {
       laypage.render({
           elem: 'page',
           count: page.RowCount,        //得到总页数
           limit: page.PageSize,         //每页显示数量
           curr: page.PageNumber + 1,   //    页码
           groups: 5,                  //连续显示分页数
           jump: function (obj, first) {
               if (!first) {//防止无限刷新
                   pageData.PageNumber = obj.curr - 1;
                   getList(page);
               }
           }
       });
   };



    //编辑按钮
   $("body").on("click", "#Update", function () {
       var _this = $(this);
       //location.href = '/ApplyMange/Edit?JobNumber=' + _this.attr("data-id");

       layer.open({
           type: 2,
           title: ['编辑信息', 'background-color: #00bb9d;text-align:center;font-size:18px;color:#FFFFFF'],
           shadeClose: true,
           shade: false,
           maxmin: false,
           area: ['1000px', '600px'],
           content: "/Message/Compile?Id="+_this.attr("data-id"),
           success: function (layero, index) {
               var body = layer.getChildFrame('body', index);//建立父窗体和子窗体关系                
               body.find('input[id="Id"]').val(_this.attr("Id"));//访问子窗体元素，
               //   注意会有延迟所以子窗体使用时要延迟加载
           },
           end: function () {
               //        如果是通过单击关闭按钮关闭弹出层，父画面没有此表单  
               if ($("#popupForm").length === 1) {
                   $("#popupForm").submit();
               }
           }
       });
   });

    //删除按钮
   $("body").on("click", "#Removetask", function () {
       var _this = $(this);
       layer.confirm('确定删除此信息？', { icon: 3, title: '提示信息' }, function (index) {
           var index = layer.msg('删除中，请稍候', { icon: 16, time: false, shade: 0.8 });
           $.ajax({
               type: "post",
               url: "/Message/Delete",    //地址
               data: { "id": _this.attr("data-id") },
               success: function (result) {
                   var i = layer.msg(result.Message, { icon: 16, time: false, shade: 0.8 });
                   getList(pageData);
                   layer.close(i);
               },
               error: function (XMLHttpRequest, textStatus, errorThrown) {
                   layer.alert(textStatus);
               }
           });
           layer.close(index);

       });
   });


    //查询
   form.on('submit(btnSelect)', function (data) {
       var Name = $("#Name").val();
       var phone = $("#Phone").val();
       var idnumber = $("#IdNumber").val();
       if (Name != null) {
           pageData.Where = "  Name like '%" + Name + "%'";
       }
       if (phone != "") {
           pageData.Where += "  and Phone='" + phone + "'";
       }
       if (idnumber !="") {
           pageData.Where += "  and IdNumber='" + idnumber + "'";
       }
       getList(pageData);
       return false;
   });
  



   $(function () {
       getList(pageData);
   });
});